/**
 * Copyright (c) 2021-2028, iron.guo 郭成杰 (jiedreams@sina.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.beluga.core.pay;

import com.alibaba.fastjson.JSON;
import com.beluga.core.model.wechat.WxPayResult;
import com.beluga.core.props.WxPayProperties;
import com.beluga.core.util.SignUtils;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * @Author iron.guo
 * @Date 2022/5/4
 * @Description
 */
public class BPayModel {

    public static  <T> T getWxPayInfo(TradeType tradeType, String prepayId,SignType signType, WxPayProperties wxPayProperties) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String nonceStr =  String.valueOf(System.currentTimeMillis());
        switch (tradeType) {
            case JSAPI:
                WxPayResult.JsapiResult jsapiResult = new WxPayResult.JsapiResult();
                jsapiResult.setAppId(wxPayProperties.getAppId());
                jsapiResult.setTimeStamp(timestamp);
                jsapiResult.setPackageValue("prepay_id=" + prepayId);
                jsapiResult.setNonceStr(nonceStr);
                //签名类型，默认为RSA，仅支持RSA。
                if (signType == null) {
                    signType = SignType.MD5;
                }
                jsapiResult.setSignType(signType.getType());
                Map<String, String> param = JSON.parseObject(JSON.toJSONString(jsapiResult), Map.class);
                jsapiResult.setPaySign(SignUtils.Sign(param, wxPayProperties.getMchKey(),signType.getType()));
                return (T) jsapiResult;
        }
        return null;
    }

    public static Map<String,String> getWxPayMap(TradeType tradeType, String prepayId,SignType signType, WxPayProperties wxPayProperties) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String nonceStr =  String.valueOf(System.currentTimeMillis());
        switch (tradeType) {
            case JSAPI:
                Map<String,String> result= Maps.newHashMap();
                result.put("appId",wxPayProperties.getAppId());
                result.put("timeStamp",timestamp);
                result.put("package","prepay_id=" + prepayId);
                result.put("nonceStr",nonceStr);
                //签名类型，默认为RSA，仅支持RSA。
                if (signType == null) {
                    signType = SignType.MD5;
                }
                result.put("signType",signType.getType());
                result.put("paySign",SignUtils.Sign(result, wxPayProperties.getMchKey(),signType.getType()));
                return result;
        }
        return null;
    }


    public static Map<String,String> getWxPayMap2(TradeType tradeType, String prepayId,SignType signType) {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String nonceStr =  String.valueOf(System.currentTimeMillis());
        switch (tradeType) {
            case JSAPI:
                Map<String,String> result= Maps.newHashMap();
                result.put("appId","wx767db2bfa6f5bbcd");
                result.put("timeStamp",timestamp);
                result.put("package","prepay_id=" + prepayId);
                result.put("nonceStr",nonceStr);
                //签名类型，默认为RSA，仅支持RSA。
                if (signType == null) {
                    signType = SignType.MD5;
                }
                result.put("signType",signType.getType());
                result.put("paySign",SignUtils.Sign(result, "xindongli88888888888888888888888",signType.getType()));
                return result;
        }
        return null;
    }


}
